﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Service.Data;
using System.Data.Common;
using Sinacor.OMM.Configuration.Router;

namespace Sinacor.OMM.Configuration.HFTMessageManager
{
    /// <summary>
    ///  Read de HFT Message Manager configuration
    /// </summary>
    public class HFTMessageManagerConfigurationReader : BaseResourceAccess
    {

        private Int32 _companyId;

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="companyId"></param>
         public HFTMessageManagerConfigurationReader(Int32 companyId)
            : base(companyId)
		{
            _companyId = companyId;
		}

		#endregion

        #region Methods
            
        /// <summary>
        ///  Read HFT Message Manager configuration by the mac address
        /// </summary>
        /// <returns></returns>
        public HFTMessageManagerConfiguration GetConfiguration(String macAddress)
        {
            //
            HFTMessageManagerConfiguration HFTConfiguration = null;
            String sqlCommand = String.Empty;

            //
            sqlCommand = @"SELECT COD_PROS_IAFR,
                                  NUM_MAC_ETRB_PROS_IAFR,
                                  COD_REPRIO,
                                  COD_ULT_MSG_PROS,
                                  NOME_DRTO_ARQ_CTRL
                            FROM TBOPROS_IAFR
                            WHERE NUM_MAC_ETRB_PROS_IAFR = :MACADDRESS ";

            using (DbCommand dbCommand = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(dbCommand, "MACADDRESS", System.Data.DbType.String, macAddress);

                HFTConfiguration = this.FindOne<HFTMessageManagerConfiguration>(dbCommand,
                    reader =>
                    {
                        HFTMessageManagerConfiguration item = new HFTMessageManagerConfiguration();

                        item.Id = reader.GetValue<Int32>("COD_PROS_IAFR");
                        item.MacAddress = reader.GetValue<String>("NUM_MAC_ETRB_PROS_IAFR");
                        item.LastMessageProcessed = reader.GetValue<String>("COD_ULT_MSG_PROS");
                        item.ControllerFilePath = reader.GetValue<String>("NOME_DRTO_ARQ_CTRL");
                        item.MessageRepository.Id = reader.GetValue<Int32>("COD_REPRIO");

                        return item;
                    });
            }

            if (HFTConfiguration != null)
            {
                HFTConfiguration.DistributionRule = GetDistributionRules(HFTConfiguration.MacAddress);

                if (HFTConfiguration.MessageRepository.Id.HasValue)
                {
                    HFTConfiguration.MessageRepository = new Sinacor.OMM.Configuration.Repository.RepositoryConfigurationReader(_companyId).GetConfiguration(HFTConfiguration.MessageRepository.Id.Value);
                }
                else
                {
                    HFTConfiguration.MessageRepository = null;
                }
            }

            //
            return HFTConfiguration;
        }

        /// <summary>
        /// Read HFT Message Manager configuration by the process id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HFTMessageManagerConfiguration GetConfiguration(Int32 id)
        {
            //
            HFTMessageManagerConfiguration HFTConfiguration = null;
            String sqlCommand = String.Empty;

            //
            sqlCommand = @" SELECT COD_PROS_IAFR,
                                   NUM_MAC_ETRB_PROS_IAFR,
                                   COD_REPRIO,
                                   COD_ULT_MSG_PROS,
                                   NOME_DRTO_ARQ_CTRL
                            FROM TBOPROS_IAFR
                            WHERE COD_PROS_IAFR = :ID ";

            using (DbCommand dbCommand = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                this.DataBase.AddInParameter(dbCommand, "ID", System.Data.DbType.Int32, id);

                HFTConfiguration = this.FindOne<HFTMessageManagerConfiguration>(dbCommand,
                    reader =>
                    {
                        HFTMessageManagerConfiguration item = new HFTMessageManagerConfiguration();

                        item.Id = reader.GetValue<Int32>("COD_PROS_IAFR");
                        item.MacAddress = reader.GetValue<String>("NUM_MAC_ETRB_PROS_IAFR");
                        item.MessageRepository.Id = reader.GetValue<Int32>("COD_REPRIO");
                        item.LastMessageProcessed = reader.GetValue<String>("COD_ULT_MSG_PROS");
                        item.ControllerFilePath = reader.GetValue<String>("NOME_DRTO_ARQ_CTRL");

                        return item;
                    });
            }

            if (HFTConfiguration != null)
            {
                HFTConfiguration.DistributionRule = GetDistributionRules(HFTConfiguration.MacAddress);

                if (HFTConfiguration.MessageRepository.Id.HasValue)
                {
                    HFTConfiguration.MessageRepository = new Sinacor.OMM.Configuration.Repository.RepositoryConfigurationReader(_companyId).GetConfiguration(HFTConfiguration.MessageRepository.Id.Value);
                }
                else
                {
                    HFTConfiguration.MessageRepository = null;
                }
            }

            //
            return HFTConfiguration;
        }


        /// <summary>
        /// Read the distribution rules 
        /// </summary>
        /// <returns></returns>
        private List<DistributionRule> GetDistributionRules(string MacAddress)
        {
            //
            String sqlCommand = String.Empty;
            List<DistributionRule> distributionRules = null;
            HFTMessageManager.HFTMessageManagerConfigurationReader hftReader = new HFTMessageManager.HFTMessageManagerConfigurationReader(_companyId);

            //
            sqlCommand = @" SELECT B.CD_CLIENTE,
                                   B.NUM_PRTA_INIC,
                                   B.NUM_PRTA_FIM
                            FROM TBOPROS_IAFR A
                            INNER JOIN TBOCNFG_DIST_MSG_IAFR B ON A.COD_PROS_IAFR = B.COD_PROS_IAFR
                            WHERE NUM_MAC_ETRB_PROS_IAFR = '" + MacAddress +  "'";

            using (DbCommand dbCommand = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                distributionRules = this.FindAll<DistributionRule>(dbCommand,
                    reader =>
                    {
                        DistributionRule item = new DistributionRule();

                        item.StartPort = reader.GetValue<Int32>("NUM_PRTA_INIC");
                        item.EndPort = reader.GetValue<Int32>("NUM_PRTA_FIM");
                        item.ClientId = reader.GetValue<Int32?>("CD_CLIENTE");

                        return item;
                    });
            }

            return distributionRules;
        }

        #endregion

    }
}
